import config from "@/config/index";
import {
	customToast
} from "./tips";
import $Storage from '@/utils/storage';
const Toast = customToast;
export function uploadFile(params) {
	const {
		fileURL,
		filePath
	} = params;
	const url = config.baseURL + fileURL;

	Toast.loading('上传中');
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url,
			filePath,
			name: 'file',
			header: {
				// "Content-Type": "multipart/form-data",
				"Authentication": $Storage.getItem('accessToken')
			},
			success: function(res) {
				const result = res.data;
				if (res.statusCode == 200) {
					resolve(JSON.parse(result))
				} else {
					reject('上传图片过大或格式错误')
					setTimeout(() => {
						Toast.fail('上传图片过大或格式错误');
					}, 500)
				}
			},
			fail: function(err) {
				console.log(err, '2')
				reject({
					data: {
						message: '上传失败！'
					}
				})
				setTimeout(() => {
					Toast.fail('上传失败!');
				}, 500)
			},
			complete: (res) => {
				setTimeout(() => {
					Toast.clear();
				}, 200)
			}
		})
	});
}
